﻿using System;
using System.IO;
using System.Windows.Forms;

namespace cs2py
{
    public partial class PynetForm : Form
    {
        public PynetForm()
        {
            InitializeComponent();
        }

        private const string py310 = @".\python310\python310.dll";
        private const string py311 = @".\python311\python311.dll";

        /// <summary>
        /// 加载py311环境
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            UsePythonnet.Init(py310);
        }

        /// <summary>
        /// 加载py310环境
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            UsePythonnet.Init(py311);
        }

        /// <summary>
        /// 检测环境信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            var msg = UsePythonnet.CheckEnvironment();
            this.rtb_msg.AppendText(msg);
        }

        /// <summary>
        /// 加载脚本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            var ofd = new OpenFileDialog();
            ofd.InitialDirectory = Environment.CurrentDirectory;
            if(ofd.ShowDialog() == DialogResult.OK)
            {
                var f = ofd.FileName;
                this.rtb_script.Text = File.ReadAllText(f);
            }
        }

        /// <summary>
        /// 执行脚本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            var scpt = this.rtb_script.Text;
            var msg = UsePythonnet.RunScript(scpt,"main");
            MessageBox.Show(msg);
        }

        /// <summary>
        /// 加载预定脚本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 释放环境
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            UsePythonnet.Shutdown();
        }
    }
}